package com.sinszm.mdb.pagebar;


import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinszm.common.exception.ApiException;
import org.springframework.util.StringUtils;

import static com.sinszm.common.util.CommonUtils.consumerWithIndex;

/**
 * Web分页组件调用工具
 *
 * @author chenjianbo
 */
public final class WebPageUtils {

    /**
     * 分页组件调用方法
     * @param select    查询器
     * @param request   请求参数
     * @param tClass    泛型类Class
     * @param <T>       泛型
     * @return          响应数据
     */
    public static <T extends WebIndex> WebPageResponse<T> doPage(ISelect select, WebPageRequest request, Class<T> tClass) {
        if (request == null) {
            throw new ApiException("PAGE-RESPONSE-ERR-001","request不能为空");
        }
        request.checkAllParameters();
        PageInfo<T> pageInfo;
        if (StringUtils.isEmpty(request.getField())
                || StringUtils.isEmpty(request.getField().trim())
                || StringUtils.isEmpty(request.getSorted())
                || StringUtils.isEmpty(request.getSorted().trim())
        ) {
            pageInfo = PageHelper.startPage(
                    request.getCurrentNum(),
                    request.getPageSize()
            ).doSelectPageInfo(select);
        } else {
            String sorted = "ASC".equalsIgnoreCase(request.getSorted()) ? "ASC" : "DESC";
            pageInfo = PageHelper.startPage(
                    request.getCurrentNum(),
                    request.getPageSize(),
                    request.getField() + " " + sorted
            ).doSelectPageInfo(select);
        }
        //格式化返回响应值
        WebPageResponse<T> response = new WebPageResponse<>(tClass);
        response.setList(pageInfo.getList());
        response.setPages(pageInfo.getPages());
        response.setTotal(pageInfo.getTotal());
        response.setPageNum(pageInfo.getPageNum());
        response.setPageSize(pageInfo.getPageSize());
        response.setPrePage(pageInfo.getPrePage());
        response.setNextPage(pageInfo.getNextPage());
        //自动计算序号
        response.getList().forEach(consumerWithIndex((data, index) -> data.setIndex(calculateIndex(response, index))));
        return response;
    }

    /**
     * 返回单据数据
     *
     * @param select    查询器
     * @param request   请求参数
     * @param tClass    泛型类Class
     * @param <T>       泛型
     * @return          响应数据
     */
    public static <T extends WebIndex> WebPageResponse.Bill<T> doPageBill(ISelect select, WebPageRequest request, Class<T> tClass) {
        WebPageResponse<T> response = doPage(select, request, tClass);
        return new WebPageResponse.Bill<>(
                response.getHeaders(),
                response.getList()
        );
    }

    /**
     * 计算序号
     * @param response  响应数据
     * @param index     当前数据循环页中的下标
     * @param <T>       泛型定义
     * @return 序号
     */
    private static <T extends WebIndex> long calculateIndex(WebPageResponse<T> response, Integer index) {
        int currentIndex = (index == null ? 0 : index) + 1;
        return response.getPageNum() <= 1 ? currentIndex : currentIndex + (response.getPageNum() - 1) * response.getPageSize();
    }

}